import { useState } from "preact/hooks";
import { sendFile } from "../api/api.ts";

// hooks/useFileUpload.ts
export const useFileUpload = () => {
  const [progress, setProgress] = useState<number | null>(null);
  const [loading, setLoading] = useState(false);

  const upload = async (formData: FormData) => {
    setLoading(true);
    setProgress(0);
    try {
      const result = await sendFile(formData, setProgress);
      return result;
    } finally {
      setLoading(false);
      setProgress(null);
    }
  };

  return { upload, progress, loading };
};